package com.example.filedemo.utils;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * the class of upfiledemo
 *
 * <p>
 * .
 *
 * @author little_lunatic
 * @date 2023-02-06
 */
public final class Doc2PdfUtils {

    private Doc2PdfUtils() {
    }

    /**
     * doc、docx 转 PDF文件
     * @param docFileName 源文件绝对路径
     * @param pdfPath 生成PDF存放路径
     */
    public static void doc2pdf(String docFileName, String pdfPath) {
        File inputFile = new File(docFileName);
        File outputFile = new File(pdfPath);
        if (!outputFile.exists()){
            try {
                outputFile.getParentFile().mkdirs();
                outputFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try (InputStream inputStream = new FileInputStream(inputFile);
             OutputStream outputStream = new FileOutputStream(outputFile)) {
            IConverter converter = LocalConverter.builder().build();
            converter.convert(inputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            converter.kill();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public static void responseBrowser(File file, HttpServletResponse response) {
        //下面的是进行响应客户端的测试代码
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            //设置Http响应头告诉浏览器下载这个附件,下载的文件名也是在这里设置的

            System.out.println("file.getName()   >>>" + file.getName());

            response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(file.getName(), "UTF-8"));
            // response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(file.getName(), "UTF-8"));
            System.out.println("file.getPath() >>>>>>" + file.getPath());

            OutputStream outputStream = null;

            outputStream = response.getOutputStream();

            byte[] bytes = new byte[2048];
            int len = 0;
            while ((len = fileInputStream.read(bytes)) > 0) {
                outputStream.write(bytes, 0, len);
            }
            fileInputStream.close();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件响应出错，读写出错");
        }

    }


    public static void onlineReader(File file, HttpServletResponse response) {
        if (file.exists()) {
            byte[] data = null;
            try {
                response.setHeader("Content-Disposition", "inline;fileName=" + URLEncoder.encode(file.getName(), "UTF-8"));
                FileInputStream input = new FileInputStream(file);
                data = new byte[input.available()];
                input.read(data);

                response.getOutputStream().write(data);
                input.close();
            } catch (Exception e) {
                System.out.println(e);
            }

        } else {
            System.out.println("文件不存在");
        }
    }
}
